using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Set, "PsSpecProject", SupportsShouldProcess = true)]
    public class SetPsSpecProjectCommand : Cmdlet
    {
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        private int _id = -1;

        [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = null;

        [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
        public String Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
        private string _description = null;

        [Parameter(Mandatory = false)]
        public String ServiceUri
        {
            get
            {
                return _serviceUri;
            }
            set
            {
                _serviceUri = value;
            }
        }
        private string _serviceUri = PsSpecCmdletUtils.Constants.ServiceUri;

        protected override void ProcessRecord()
        {
            // Check that they specified a valid project name
            if ((! String.IsNullOrEmpty(Name)) && (!PsSpecCmdletUtils.IsValidProjectName(Name)))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.InvalidProjectName),
                    "INVALID_PROJECT_NAME",
                    ErrorCategory.InvalidArgument, Name));
                return;
            }

            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.UpdatePsSpecProjectShouldProcess, Id)))
            {
                // Update the project
                PsSpecCmdletService.CmdletService service =
                    new PsSpecCmdletService.CmdletService();
                service.Url = ServiceUri;
                service.UseDefaultCredentials = true;

                try
                {
                    service.UpdateProject(Id, Name, Description);
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "PROJECT_SERVICE_ERROR", ErrorCategory.InvalidOperation, Id));
                }
            }
        }
    }
}
